×
☰ See All Chapters

Java Wrapper classes - Autoboxing and Unboxing

Wrapper classes are used to convert any primitive data type into an object. The primitive data types are not objects; they do not belong to any class; they are defined in the language itself. Sometimes, it is required to convert data types into objects in Java language. For example, upto JDK1.4, the data structures accept only objects to store. A data type is to be converted into an object and then added to a Stack or Vector etc. For this conversion, the designers introduced wrapper classes. Java is an object-oriented language and can view everything as an object.

  1. A simple file can be treated as an object using java.io.File 

  2. An address of a system can be seen as an object using java.util.URL. 

  3. An image can be treated as an object using java.awt.Image and a  

  4. Simple primitive data type can be converted into an object using wrapper classes. 

Wrapper classes, Boxing/Autoboxing, and Unboxing

 A wrapper class wraps (encloses) around a data type and gives it an object appearance, this automatic conversion of primitive data types into equivalent wrapper type is known as Boxing/Autoboxing. Wherever, the data type is required as an object, this object can be used.

Wrapper classes include methods to unwrap the object and give back the primitive data type. This operation of converting objects to primitive data types is known as  Unboxing.

java-wrapper-classes-0
 

List of Wrapper classes

In the above code, Integer class is known as a wrapper class (because it wraps around int data type to give it an impression of object). To wrap (or to convert) each primitive data type, there comes a wrapper class. Eight wrapper classes exist in java.lang package that represent 8 data types. Following list gives.

Primitive data type

Wrapper class

byte

Byte

short

Short

int

Ineger

long

Long

float

Float

double

Double

Char

Character

boolean

Boolean

All the 8 wrapper classes are placed in java.lang package so that they are implicitly imported and made available to the programmer. As you can observe in the above hierarchy, the super class of all numeric wrapper classes is Number and the super class for Character and Boolean is Object. All the wrapper classes are defined as final and thus designers prevented them from inheritance.

Autoboxing/Unboxing in Expressions

public class AutoBoxDemo {

        public static void main(String args[]) {

                Integer intObj1, intObj2;

                int i;

                intObj1 = 100;

                System.out.println("Original value of iOb: " + intObj1);

                ++intObj1;

                System.out.println("After ++intObj1: " + intObj1);

                intObj2 = intObj1 + (intObj1 / 3);

                System.out.println("intObj2 after expression: " + intObj2);

                i = intObj1 + (intObj1 / 3);

                System.out.println("i after expression: " + i);

        }

}

Output:

Original value of iOb: 100

After ++intObj1: 101

intObj2 after expression: 134

i after expression: 134

In the program, pay special attention to this line:

++intObj1;

This causes the value in intObj1 to be incremented. It works like this: intObj1 is unboxed, the value is incremented, and the result is reboxed.

Auto-unboxing also allows you to mix different types of numeric objects in an expression. Once the values are unboxed, the standard type promotions and conversions are applied. For example, the following program is perfectly valid:

class AutoBoxDemo {

public static void main(String args[]) {

Integer iOb = 100;

Double dOb = 98.6;

dOb = dOb + iOb;

System.out.println("dOb after expression: " + dOb);

}       

}

Because of auto-unboxing, you can use integer numeric objects to control a switch statement. For example, consider this fragment:

Integer iOb = 2;

switch(iOb) {

case 1: System.out.println("one");

break;

case 2: System.out.println("two");

break;

default: System.out.println("error");

}

Autoboxing/Unboxing Boolean in Character Values

public class AutoBoxDemo {

        public static void main(String args[]) {

                Boolean b = true;

                if (b) {

                        System.out.println("b is true");

                }

                Character charWrapper = 'x';

                char charPrimitive = charWrapper;

                System.out.println("charPrimitive is " + charPrimitive);

        }

}

Output:

b is true

charPrimitive is x

 

We should restrict your use of the type wrappers to only those cases in which an object representation of a primitive type is required. Autoboxing/unboxing was not added to Java as a “back door” way of eliminating the primitive types.

 


All Chapters
Author